//
//  RegisterRuleViewController.m
//  GYMWinners
//
//  Created by Logic Solutions on 4/1/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "RegisterRuleViewController.h"
#import "RegisterRule2ViewController.h"

#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "SBJSON.h"
#import "MBProgressHUD.h"
#import "BaseroutAPI.h"
#import "AppDelegate.h"

@interface RegisterRuleViewController ()

@end

@implementation RegisterRuleViewController
@synthesize daysPickerView;
@synthesize daysToGymPerWeekArr;

-(IBAction) nextStep{
    
    RegisterRule2ViewController* rule2=[[RegisterRule2ViewController alloc]init];
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.8];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.navigationController.view cache:NO];
    
    [self.navigationController pushViewController:rule2 animated:YES];
    
    [UIView commitAnimations];

}

-(IBAction)back {
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.8];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.navigationController.view cache:NO];
    [self.navigationController popViewControllerAnimated:NO];
    [UIView commitAnimations];
    
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem = newBackButton; 
    
    NSMutableDictionary *slist = [[NSMutableDictionary alloc] init];
    [self zSendServer:slist theTitle:@"请稍等" theCommand:@"/register/list"];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - delegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ //返回列数
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ //返回每列的最大行数
    NSArray *commitsArr = (NSArray *)[[BaseroutAPI sharedInstance] zGetObj:@"commitsArr"];
    return [commitsArr count];
}

//每一列中每一行的具体内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ 

    NSArray *commitsArr = (NSArray *)[[BaseroutAPI sharedInstance] zGetObj:@"commitsArr"];
    NSDictionary *tmp = [commitsArr objectAtIndex:row];
    NSString *pickerRowTitle = [NSString stringWithFormat:@"每周%@天", [tmp objectForKey:@"commits"]];
    
    return pickerRowTitle;
}

- (void) updateCommitID:(NSInteger)row{
    NSArray *commitsArr = (NSArray *)[[BaseroutAPI sharedInstance] zGetObj:@"commitsArr"];
    NSDictionary *commit = [commitsArr objectAtIndex:row];
    NSString *commitDays = [NSString stringWithFormat:@"%@", [commit objectForKey:@"commits"]];
    NSString *commitedID = [NSString stringWithFormat:@"%@", [commit objectForKey:@"id"]];
    
    NSLog(@"row=%d, commitID=%@, commitDays=%@", row, commitedID, commitDays);
    
    [[BaseroutAPI sharedInstance] zSetVar:@"selectedCommitID" theValue:commitedID];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ //选中哪一列哪一行
    
    [self updateCommitID:row];
    
}

#pragma mark - ASIFormDataRequest delegate

- (void)requestFinished:(ASIHTTPRequest *)request
{    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    if (request.responseStatusCode == 200) {
        
        NSString *responseString = [request responseString];
        NSLog(@"responseString=[%@]", responseString);   
        responseString = [responseString stringByReplacingOccurrencesOfString:@"null" withString:@"\"\""];
        NSLog(@"responseString=[%@]", responseString);  
        NSDictionary *responseDict = [responseString JSONValue];

        NSArray *commitsArr = [responseDict objectForKey:@"commits"];
        NSArray *stakesArr = [responseDict objectForKey:@"stakes"];
        NSArray *cardsArr = [responseDict objectForKey:@"cards"];

        if( [commitsArr count] > 0 &&  [stakesArr count] > 0 && [cardsArr count] > 0)
        {
            [[BaseroutAPI sharedInstance] zSetObj:@"commitsArr" theValue:commitsArr];
            [[BaseroutAPI sharedInstance] zSetObj:@"stakesArr" theValue:stakesArr];
            [[BaseroutAPI sharedInstance] zSetObj:@"cardsArr" theValue:cardsArr];
            

            //daysToGymPerWeekArr= [[NSArray alloc]initWithObjects:@"每周1天",@"每周2天",@"每周3天",@"每周4天",@"每周5天",@"每周6天",@"每周7天", nil];
            daysPickerView= [[UIPickerView alloc] initWithFrame:CGRectMake(0,110, 320, 200)];    
            daysPickerView.delegate=self;
            daysPickerView.showsSelectionIndicator=YES;
            [daysPickerView selectRow:0 inComponent:0 animated:YES];
            [self updateCommitID:0];
            [self.view addSubview:daysPickerView];
        }
        else
        {
            [[BaseroutAPI sharedInstance] MessageBox:@"提示" Message:@"登录失败"]; 
        }
        
    } else {
        NSLog(@"Unexpected error");  
        [[BaseroutAPI sharedInstance] MessageBox:@"提示" Message:@"通讯失败"];
    }
    
}

- (void)requestFailed:(ASIHTTPRequest *)request
{    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    NSError *error = [request error];
    NSLog(@"Error=%@", error.localizedDescription);   
    
    [[BaseroutAPI sharedInstance] MessageBox:@"提示" Message:@"通讯失败"];
}

@end
